<template>
  <button :class="{[`icon-${iconPosition}`]:true}" class="p-button">
    <p-icon v-if="icon && !loading" :name="icon"></p-icon>
    <p-icon v-if="loading" :name="icon" class="loading"></p-icon>
    <span class="content">
    <slot></slot>
    </span>
  </button>
</template>
<script>
export default {
  props: {
    icon: {
      type: String,
      default: ''
    },
    iconPosition: {
      icon: String,
      default: 'left',
      validate(value) {
        return ['left', 'right'].includes(value)
      }
    },
    loading: {
      type: Boolean,
      default: false
    }
  }
}
</script>

<style lang="scss">
@keyframes spin {
  0% {
    transform: rotate(0deg)
  }
  100% {
    transform: rotate(360deg)
  }
}

.p-button {
  font-size: var(--font-size);
  height: var(--button-height);
  padding: 0 1em;
  border-radius: var(--border-radius);
  border: 1px solid var(--border-color);
  background: var(--button-bg);
  display: inline-flex;
  justify-content: center;
  align-items: center;
  // 处理 inline-flex 导致不对齐
  vertical-align: middle;

  &:hover {
    border-color: var(--border-color-hover);
  }

  &:active {
    background-color: var(--button-active-bg);
  }

  &:focus {
    outline: none;
  }

  > .icon {
    order: 1;
    margin-right: .3em;
  }

  > .content {
    order: 2;
  }

  &.icon-right {
    > .icon {
      order: 2;
      margin-right: 0;
      margin-left: .3em;
    }

    > .content {
      order: 1;
    }
  }

  .loading {
    animation: spin 2s infinite linear;
  }
}

</style>